/**
 * Created by duwei on 2019/4/11.
 */
/**
 * 字牌测试
 * */
var TestZiPai = cc.Layer.extend({
    _type: null,
    _curPos: 0,
    _numObj: {},
    ctor: function () {
        this._super();
        loadFile(this, zipai.res.TestZiPai_json);
        this.rootNode.x = cc.winSize.width / 2;
        this.rootNode.y = cc.winSize.height / 2;

        this._initView();
        this._addTouchListener();
        this._refreshSet();
    },
    _initView: function () {
        for (var i = 0; i < 4; i++) {
            for (var j = 0; j < 30; j++) {
                safeHide(this['_pos' + i]['card' + j]);
            }
            this._numObj['_pos' + i] = [];
        }
    },

    _pushPai: function (index) {
        var id = index + 1;
        var length = this._curPos<3?20:30;
        if (id > 0 && this._numObj['_pos' + this._curPos].length < length) {
            this._numObj['_pos' + this._curPos].push(id);
            this._refreshView();
        }
    },
    _pushPaiByID: function (id) {
        var length = this._curPos<3?20:30;
        if (id > 0 && this._numObj['_pos' + this._curPos].length < length) {
            this._numObj['_pos' + this._curPos].push(id);
            this._refreshView();
        }
    },
    setPaiHua:function (sprite, i) {
        var cardname = 'mid_' + i;
        var name = mCard.getCardImg(cardname);
        for (var i = 0; i < 3; i++) {
            var frame = cc.spriteFrameCache.getSpriteFrame(name);
            if (!frame) {
                cc.spriteFrameCache.addSpriteFrames(zipai.res[mCard.getCardstr()+'_plist']);
            } else {
                sprite.setSpriteFrame(frame);
                sprite.setVisible(true);
                break;
            }
        }
    },
    _refreshSet: function () {
        for (var i = 0; i < 20; i++) {
            var card  = this['_set']['card' + i];
            this.setPaiHua(card,i+1);
        }
    },

    _setPai: function (type, idx) {
        var frame = cc.spriteFrameCache.getSpriteFrame(name);
        // 1万 p2l1.png
        // 1条 p2l10.png
        // 1筒 p2l19.png
    },

    /**
     * 添加触摸事件
     * @private
     * */
    _addTouchListener: function () {
        var self = this;
        TouchUtil.setOnclickListener(this['_btn_close'], function () {
            self.removeFromParent();
        });
        TouchUtil.setOnclickListener(this['_btn_send'], function () {
            var data = {};
            data['paiArrs'] = [];
            for (var s = 0; s < 3; s++) {
                data['paiArrs'].push({
                    pos: s, paiArr: self._numObj['_pos' + s]
                });
            }

            data['unusedPaiArr'] = self._numObj['_pos3'];
            data['room_id'] = gameData.roomId;
            console.log('配牌器：' + JSON.stringify(data));
            network.send(3999, data);
            self.removeFromParent();
        });
        for (var idx = 0; idx < 20; idx++) {
            (function (idx) {
                TouchUtil.setOnclickListener(self['_set']['card' + idx], function () {
                    self._pushPai(idx);
                });
            })(idx);
        }
        for (var index = 0; index < 4; index++) {
            (function (index) {
                TouchUtil.setOnclickListener(self['_pos' + index]['set'], function () {
                    self._curPos = index;
                });
                TouchUtil.setOnclickListener(self['_pos' + index]['delete'], function () {
                    self._numObj['_pos' + index].pop();
                    self._refreshView();
                });
            })(index);
        }

        TouchUtil.setOnclickListener(self['_btn_input_ok'], function () {
            var str = self['_input'].getString()
            var arr = str.split(',')
            for (var i=0; i<arr.length; i++) {
                self._pushPaiByID(parseInt(arr[i]));
            }
        });
    },

    _refreshView: function () {
        var self = this;
        for (var m = 0; m < 4; m++) {
            for (var n = 0; n < 30; n++) {
                safeHide(this['_pos' + m]['card' + n]);
            }
        }
        for (var key in this._numObj) {
            if (this._numObj.hasOwnProperty(key)) {
                var cards_arr = this._numObj[key];
                // console.log(key + '=-------', cards_arr);
                for (var i = 0; i < cards_arr.length; i++) {
                    (function (i) {
                        var card = self[key]['card' + i];
                        self.setPaiHua(card,cards_arr[i]);
                    })(i);
                }
            }
        }
    }
});